package find;

import dto.ImageDto;
import my.opencv.CascadeClassifierUtils;
import my.opencv.Mat2BufImgUtils;
import my.opencv.OpencvFileUtils;
import my.opencv.OpencvImageUtils;
import opencv.utils.InitUtils;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.utils.Converters;
import xiugai.other.OCRUtil;
import xiugai.other.OpencvUtil;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

import static org.opencv.imgcodecs.Imgcodecs.imwrite;
import static other.OpencvUtil.shear;

public class TestIDCard {
    public static final ThreadLocal<Map<String,Rect>> threadLocal = new ThreadLocal<>();
    static {
        InitUtils.addLibrary();
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    static String imgPath="C:\\Users\\admin\\Desktop\\cut\\0.jpg";
    static String savePath="C:\\Users\\admin\\Desktop\\cut\\";

    public static void main(String[] args) throws IOException {
        new TestIDCard().testUp();
    }

    public static void testDown() {

    }




    public void testUp() throws IOException {
        threadLocal.set(new HashMap());
        Mat mat = OpencvFileUtils.readChinesePath("C:\\Users\\admin\\Desktop\\shengfenzheng\\800x533-202701_v2_11721438432021704_d3e43c3de72561963e5da72dd3838293.jpg");
        //循环进行人脸识别,校正图片方向
//        Mat gray=new Mat();
//        Imgproc.cvtColor(mat,gray, Imgproc.COLOR_BGR2GRAY);
//        imwrite(savePath+"grayfirst.jpg",gray);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/read.jpg", mat);
        ImageDto idCard= CascadeClassifierUtils.correctIDCardUp(mat);
        if(idCard == null){
            System.out.println("未检测到人脸");
            Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/noface.jpg", mat);
//            Mat gray=new Mat();
//            Imgproc.cvtColor(mat,gray, Imgproc.COLOR_BGR2GRAY);
//            Mat threshold=new Mat();
//            Imgproc.blur( gray, gray,new Size(1,1));
//            Imgproc.threshold(gray,threshold,160,255, Imgproc.THRESH_BINARY);
//            imwrite(savePath+"zfx_threshold1.jpg",threshold);
            OCRUtil.getImageMessage(Mat2BufImgUtils.Mat2BufImg(mat,".jpg"),"");
            return;
        }
        contours(idCard);
    }


    public void contours(ImageDto imageDto) {
        Mat src = imageDto.getMat().clone();

        int image_with = src.width();
        int image_hight = src.height();
        //灰度化+二值化
        Mat gray=new Mat();
        Imgproc.cvtColor(src,gray, Imgproc.COLOR_BGR2GRAY);
        Imgproc.blur( gray, gray,new Size(1,1));
        imwrite(savePath+"zfx_gray1.jpg",gray);
        Mat threshold=new Mat();
        Imgproc.threshold(gray,threshold,160,255, Imgproc.THRESH_BINARY);
        imwrite(savePath+"zfx_threshold1.jpg",threshold);
        System.out.println("二值化通道数="+threshold.channels());

        //轮廓查找
        List<MatOfPoint> contours=new ArrayList<>();
        Mat hierarchy = new Mat();
        Imgproc.findContours(threshold,contours,hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_NONE);
        Iterator<MatOfPoint> items = contours.iterator();
        while (items.hasNext()){
            MatOfPoint it = items.next();
            if(it.t().height()<=2 && it.t().width()<=2){
                items.remove();
            }
        }
        System.out.println("轮廓数量："+contours.size());
        System.out.println("轮廓类型："+hierarchy);




            Imgproc.drawContours(src,contours,-1,new Scalar(0,0,255),10);
        imwrite(savePath+"zfx_hierarchy.jpg",hierarchy);
        imwrite(savePath+"zfx_xxx.jpg",src);




//        //灰度化+二值化
        gray=new Mat();
        Imgproc.cvtColor(src,gray, Imgproc.COLOR_BGR2GRAY);
        Imgproc.blur( gray, gray,new Size(3,3));
        imwrite(savePath+"zfx_gray.jpg",gray);
        threshold=new Mat();
        Imgproc.threshold(gray,threshold,100,255, Imgproc.THRESH_BINARY);
        imwrite(savePath+"zfx_threshold.jpg",threshold);
        System.out.println("二值化通道数="+threshold.channels());

        //轮廓查找
        contours=new ArrayList<>();
        hierarchy = new Mat();
        Imgproc.findContours(threshold,contours,hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_NONE);
        System.out.println("轮廓数量："+contours.size());
        System.out.println("轮廓类型："+hierarchy);


        Imgproc.drawContours(src,contours,-1,new Scalar(0,0,255),5);
        imwrite(savePath+"zfx_hierarchy2.jpg",hierarchy);
        imwrite(savePath+"zfx_xxx2.jpg",src);

        Mat old2 = imageDto.getMat().clone();
        items = contours.iterator();
        int index = 0;

        Map<String,Rect>  mapRect =  threadLocal.get();
        Rect tou = mapRect.get("touxiang");
        List<Rect> rects = new ArrayList<>();

        Mat draw = old2.clone();
        while (items.hasNext()){
            MatOfPoint it = items.next();
            Rect rect= Imgproc.boundingRect(it);
            //去太小 太大
            if((rect.height<=10 && rect.width<=10) /*|| (rect.height>=old2.height()/3 && rect.width>=old2.width()/3)*/){
                items.remove();
                continue;
            }
            //去头像
            if(((rect.x >= tou.x) ||(rect.x<=tou.x && (rect.x+rect.width)>=tou.x)) &&(rect.y<tou.y+tou.height)){
                if(!(rect.x < (tou.x-tou.width/3))){
                    items.remove();
                    continue;
                }
            }
            System.out.println("  22x:"+rect.x+"  y:"+rect.y+"  widyh:"+rect.width+"  height:"+rect.height);
            rects.add(rect);
            draw = OpencvImageUtils.drawRect(rect,draw);

        }
        //对users按年龄进行排序
        Collections.sort(rects, new Comparator<Rect>() {

            @Override
            public int compare(Rect o1, Rect o2) {
                // 升序
                return -(o1.y-o2.y);
            }
        });
        getPositionsOfIDCard(old2,rects);







    }

    static Map<String,Rect> pointRects =  new HashMap<>();
    static int  码_X =  0;
    private Map<String,Mat> getPositionsOfIDCard(Mat oragin,List<Rect> points){
        Map<String,Rect> rectIbMap= new HashMap<>();
        Map<String,Rect>  mapRect =  threadLocal.get();
        Rect tou = mapRect.get("touxiang");
        Rect eys = mapRect.get("eyesRects");
        for(int i = 0;i<points.size();i++){
            String name = OCRUtil.getImageMessage(Mat2BufImgUtils.Mat2BufImg(new Mat(oragin,points.get(i)),".jpg"),"");
            Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/"+(i)+".jpg", new Mat(oragin,points.get(i)));
        }



        Rect codeRect = codeRect(oragin,points);
        if(codeRect!=null){
            pointRects.put("codeRect",codeRect);
            OCRUtil.getImageMessage(Mat2BufImgUtils.Mat2BufImg(new Mat(oragin,codeRect),".jpg"),"");
        }
        Rect codeNameRect = codeNameRect(oragin,points);
        if(codeNameRect!=null){
            pointRects.put("codeNameRect",codeNameRect);
            OCRUtil.getImageMessage(Mat2BufImgUtils.Mat2BufImg(new Mat(oragin,codeNameRect),".jpg"),"");

        }

        Rect codeAddressRect = codeAddressRect(oragin,points);
        if(codeAddressRect!=null){
            pointRects.put("codeAddressRect",codeAddressRect);
            OCRUtil.getImageMessage(Mat2BufImgUtils.Mat2BufImg(new Mat(oragin,codeAddressRect),".jpg"),"");
        }
        Rect codeAddressNameRect = codeAddressNameRect(oragin,points);
        if(codeAddressNameRect!=null){
            pointRects.put("codeAddressNameRect",codeAddressNameRect);
            OCRUtil.getImageMessage(Mat2BufImgUtils.Mat2BufImg(new Mat(oragin,codeAddressNameRect),".jpg"),"");
        }

        Rect birthRect = birthRect(oragin,points);
        if(birthRect!=null){
            pointRects.put("birthRect",birthRect);
            OCRUtil.getImageMessage(Mat2BufImgUtils.Mat2BufImg(new Mat(oragin,birthRect),".jpg"),"");
        }
        Rect birthInfoRect = birthInfoRect(oragin,points);
        if(birthInfoRect!=null){
            pointRects.put("birthInfoRect",birthInfoRect);
            OCRUtil.getImageMessage(Mat2BufImgUtils.Mat2BufImg(new Mat(oragin,birthInfoRect),".jpg"),"");
        }

        Rect sexRect = sexRectColumn(oragin,points);
        if(sexRect!=null){
            pointRects.put("sexRect",sexRect);
            OCRUtil.getImageMessage(Mat2BufImgUtils.Mat2BufImg(new Mat(oragin,sexRect),".jpg"),"");
        }

        Rect nameRectColumn = nameRectColumn(oragin,points);
        if(nameRectColumn!=null){
            pointRects.put("nameRectColumn",nameRectColumn);
            OCRUtil.getImageMessage(Mat2BufImgUtils.Mat2BufImg(new Mat(oragin,nameRectColumn),".jpg"),"");
        }

        return null;

    }


    public Rect sexRectColumn(Mat oragin,List<Rect> points){
        Rect birthRect = pointRects.get("birthRect");
        Rect codeAddressRect = pointRects.get("codeAddressRect");
        Rect codeAddressNameRect = pointRects.get("codeAddressNameRect");

        int x = birthRect.x;
        int y = birthRect.y - (codeAddressRect.y - birthRect.y) ;
        int h = birthRect.height;
        int w = codeAddressRect.x+codeAddressRect.width - codeAddressNameRect.x;
        Rect sexRect=  new Rect(x,y,w,h);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/sexRect.jpg", new Mat(oragin,sexRect));
        removeUselessRect(points,sexRect);


        return sexRect;
    }

    public Rect nameRectColumn(Mat oragin,List<Rect> points){
        Rect sexRect = pointRects.get("sexRect");
        Rect codeAddressRect = pointRects.get("codeAddressRect");
        Rect codeAddressNameRect = pointRects.get("codeAddressNameRect");

        Iterator<Rect> items = points.iterator();
        List<Rect> list = new ArrayList<>();
        int x = Integer.MAX_VALUE;
        int h = 0;
        int y = Integer.MAX_VALUE;
        int w = 0;
        while (items.hasNext()){
            Rect rect= items.next();
            if(w ==0){
                w = rect.width;
            }
           if(rect.x<x){
               x = rect.x;
           }
           if(rect.y<y){
               y = rect.y;
           }
           if(rect.height>h){
               h = rect.height;
           }
           if(rect.x+rect.width-x>w){
               w = rect.x+rect.width-x;
           }
        }

        Rect nameRectColumn=  new Rect(x,y,w,h);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/birthRect.jpg", new Mat(oragin,nameRectColumn));
        return nameRectColumn;
    }
    public Rect birthInfoRect(Mat oragin,List<Rect> points){
        Rect birthRect = pointRects.get("birthRect");
        Rect codeAddressRect = pointRects.get("codeAddressRect");

        int x = codeAddressRect.x;
        int y = birthRect.y;
        int h = birthRect.height;
        int w = codeAddressRect.width;
        Rect birthInfoRect=  new Rect(x,y,w,h);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/birthRect.jpg", new Mat(oragin,birthInfoRect));
        removeUselessRect(points,birthRect);
        return birthInfoRect;
    }


    public Rect birthRect(Mat oragin,List<Rect> points){
        Rect codeAddressNameRect = pointRects.get("codeAddressNameRect");
        Rect codeAddressRect = pointRects.get("codeAddressRect");
        Iterator<Rect> items = points.iterator();
        List<Rect> list = new ArrayList<>();
        while (items.hasNext()){
            Rect rect= items.next();
            if(rect.y+rect.height > codeAddressNameRect.y - codeAddressNameRect.height   &&  rect.x+rect.width <codeAddressRect.x){
                System.out.println("birth");

                items.remove();
                list.add(rect);
                continue;
            }
        }
        System.out.println("birthRect size:"+list.size());
        Rect address = null;
        if(list.size() ==1) {
            address = list.get(0);
        }else if(list.size() ==2){
//            if(list.get(1).y - list.get(0).y < list.get(1).width    && )
            int x = list.get(1).x - list.get(0).x >0?list.get(0).x: list.get(1).x;
            int y = list.get(1).y - list.get(0).y >0?list.get(0).y: list.get(1).y;
            int h = list.get(1).y+list.get(1).height > list.get(0).y +list.get(0).height?list.get(1).height: list.get(0).height;
            int w = list.get(1).x - list.get(0).x >0?list.get(1).x+list.get(1).width - list.get(0).x: list.get(0).x+list.get(0).width -list.get(1).x;
            address = new Rect(x,y,w,h);
            Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/birthRect.jpg", new Mat(oragin,address));
        }
        removeUselessRect(points,address);
        return address;
    }


    public Rect codeAddressNameRect(Mat oragin,List<Rect> points){
        Rect codeAddressRect = pointRects.get("codeAddressRect");
        Iterator<Rect> items = points.iterator();
        List<Rect> list = new ArrayList<>();
        while (items.hasNext()){
            Rect rect= items.next();
            if(rect.y+rect.height > codeAddressRect.y   &&  rect.x+rect.width <codeAddressRect.x){
                System.out.println("地址");

                items.remove();
                list.add(rect);
                continue;
            }
        }
        System.out.println("codeAddressNameRect size:"+list.size());
        Rect address = null;
        if(list.size() ==1) {
            address = list.get(0);
        }else if(list.size() ==2){
//            if(list.get(1).y - list.get(0).y < list.get(1).width    && )
            int x = list.get(1).x - list.get(0).x >0?list.get(0).x: list.get(1).x;
            int y = list.get(1).y - list.get(0).y >0?list.get(0).y: list.get(1).y;
            int h = list.get(1).y+list.get(1).height > list.get(0).y +list.get(0).height?list.get(1).height: list.get(0).height;
            int w = list.get(1).x - list.get(0).x >0?list.get(1).x+list.get(1).width - list.get(0).x: list.get(0).x+list.get(0).width -list.get(1).x;
            address = new Rect(x,y,w,h);
            Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/codeAddressNameRect.jpg", new Mat(oragin,address));
        }
        removeUselessRect(points,address);
        return address;
    }

    public Rect codeAddressRect(Mat oragin,List<Rect> points){
        // start  身 份
        Map<String,Rect>  mapRect =  threadLocal.get();
        Rect codeNameRect = pointRects.get("codeNameRect");
        Rect address = null;
        Iterator<Rect> items = points.iterator();
        while (items.hasNext()){
            Rect rect= items.next();
            if(rect.width > codeNameRect.width){
                System.out.println("地址详细");
                address = rect;
                Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/idcardNum.jpg", new Mat(oragin,rect));
                items.remove();
                continue;
            }
        }
        return address;
    }
    public Rect codeRect(Mat oragin,List<Rect> points){
        Map<String,Rect>  mapRect =  threadLocal.get();
        Rect tou = mapRect.get("touxiang");
        Rect num = null;

        Iterator<Rect> items = points.iterator();
        while (items.hasNext()){
            Rect rect= items.next();
            if(rect.y<= tou.y+tou.height){
                break;
            }
            if(rect.y > (tou.y+tou.height)  && (rect.x+rect.width>tou.x) && rect.width*2>oragin.width()){
                System.out.println("身份证号码数字");
                num = rect;
                Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/idcardNum.jpg", new Mat(oragin,rect));
                items.remove();
                continue;
            }
        }
        return num;
    }
    public Rect codeNameRect(Mat oragin,List<Rect> points){
        Map<String,Rect>  mapRect =  threadLocal.get();
        Rect tou = mapRect.get("touxiang");
        Rect codeRect = pointRects.get("codeRect");
        if(null == codeRect){
            return null;
        }
        Rect codeName = null;

        Iterator<Rect> items = points.iterator();
        while (items.hasNext()){
            Rect rect= items.next();
            if(rect.y<= tou.y+tou.height && (rect.x+rect.width)>codeRect.x){
                break;
            }
            if(rect.y+rect.height > codeRect.x  && (rect.width)*5/2 >codeRect.width){
                codeName = rect;
                System.out.println("身份证号码");
                Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/idcardName.jpg", new Mat(oragin,rect));
                码_X = rect.x+rect.width;
               items.remove();
                continue;
            }
        }

        //remove
        items = points.iterator();
        while (items.hasNext()){
            Rect rect=items.next();
            if(rect.y+rect.height>codeRect.y){
                items.remove();
            }
        }
        return codeName;
    }

    /**
     * y大于
     * @param points
     * @param rect
     */
    private void removeUselessRect(List<Rect> points,Rect rect0){
        Iterator<Rect> items = points.iterator();
        items = points.iterator();
        while (items.hasNext()){
            Rect rect=items.next();
            if(rect.y+rect.height>rect0.y){
                items.remove();
            }
        }
    }



    // 轮廓重合合并

    private Byte[][] progos = {
            {0, 0, 0, 0},
            {0, 0, 0, 0},
            {0, 0, 0, 0},
            {0, 0, 0, 0},
            {0, 0, 0, 0}
    };

    /**
     * h:15
     * w:30
     */
    private Byte[][] empty = {
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    };
    /**
     * h:15
     * w:30
     */
    private Byte[][] matcher = {
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    };


    public static String name(Mat mat){

        Point point1=new Point(mat.cols()*0.18,mat.rows()*0.11);
        Point point2=new Point(mat.cols()*0.18,mat.rows()*0.24);
        Point point3=new Point(mat.cols()*0.4,mat.rows()*0.11);
        Point point4=new Point(mat.cols()*0.4,mat.rows()*0.24);
        List<Point> list=new ArrayList<>();
        list.add(point1);
        list.add(point2);
        list.add(point3);
        list.add(point4);
        Mat name= shear(mat,list);
        name= OpencvUtil.drawContours(name,50);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/test_name.jpg", name);
        BufferedImage nameBuffer= OpencvUtil.Mat2BufImg(name,".jpg");
        String nameStr= OCRUtil.getImageMessage(nameBuffer,"chi_sim");
        nameStr=nameStr.replace("\n","");
        return nameStr+"\n";
    }

    public static String sex(Mat mat){
        Point point1=new Point(mat.cols()*0.18,mat.rows()*0.25);
        Point point2=new Point(mat.cols()*0.18,mat.rows()*0.35);
        Point point3=new Point(mat.cols()*0.25,mat.rows()*0.25);
        Point point4=new Point(mat.cols()*0.25,mat.rows()*0.35);
        List<Point> list=new ArrayList<>();
        list.add(point1);
        list.add(point2);
        list.add(point3);
        list.add(point4);
        Mat sex= shear(mat,list);
        sex= OpencvUtil.drawContours(sex,50);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/sex.jpg", sex);
        BufferedImage sexBuffer= OpencvUtil.Mat2BufImg(sex,".jpg");
        String sexStr= OCRUtil.getImageMessage(sexBuffer,"chi_sim");
        sexStr=sexStr.replace("\n","");
        return sexStr+"\n";
    }

    public static String nation(Mat mat){
        Point point1=new Point(mat.cols()*0.39,mat.rows()*0.25);
        Point point2=new Point(mat.cols()*0.39,mat.rows()*0.36);
        Point point3=new Point(mat.cols()*0.55,mat.rows()*0.25);
        Point point4=new Point(mat.cols()*0.55,mat.rows()*0.36);
        List<Point> list=new ArrayList<>();
        list.add(point1);
        list.add(point2);
        list.add(point3);
        list.add(point4);
        Mat nation= shear(mat,list);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/nation.jpg", nation);
        BufferedImage nationBuffer= OpencvUtil.Mat2BufImg(nation,".jpg");
        String nationStr= OCRUtil.getImageMessage(nationBuffer,"chi_sim");
        nationStr=nationStr.replace("\n","");
        return nationStr+"\n";
    }

    public static String birthday(Mat mat){
        Point point1=new Point(mat.cols()*0.18,mat.rows()*0.35);
        Point point2=new Point(mat.cols()*0.18,mat.rows()*0.35);
        Point point3=new Point(mat.cols()*0.55,mat.rows()*0.48);
        Point point4=new Point(mat.cols()*0.55,mat.rows()*0.48);
        List<Point> list=new ArrayList<>();
        list.add(point1);
        list.add(point2);
        list.add(point3);
        list.add(point4);
        Mat birthday= shear(mat,list);
        birthday= OpencvUtil.drawContours(birthday,50);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/birthday.jpg", birthday);
        //年份
        Point yearPoint1=new Point(0,0);
        Point yearPoint2=new Point(0,birthday.rows());
        Point yearPoint3=new Point(birthday.cols()*0.29,0);
        Point yearPoint4=new Point(birthday.cols()*0.29,birthday.rows());
        List<Point> yearList=new ArrayList<>();
        yearList.add(yearPoint1);
        yearList.add(yearPoint2);
        yearList.add(yearPoint3);
        yearList.add(yearPoint4);
        Mat year= shear(birthday,yearList);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/year.jpg", year);
        BufferedImage yearBuffer= OpencvUtil.Mat2BufImg(year,".jpg");
        String yearStr= OCRUtil.getImageMessage(yearBuffer,"eng");

        //月份
        Point monthPoint1=new Point(birthday.cols()*0.44,0);
        Point monthPoint2=new Point(birthday.cols()*0.44,birthday.rows());
        Point monthPoint3=new Point(birthday.cols()*0.55,0);
        Point monthPoint4=new Point(birthday.cols()*0.55,birthday.rows());
        List<Point> monthList=new ArrayList<>();
        monthList.add(monthPoint1);
        monthList.add(monthPoint2);
        monthList.add(monthPoint3);
        monthList.add(monthPoint4);
        Mat month= shear(birthday,monthList);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/month.jpg", month);
        BufferedImage monthBuffer= OpencvUtil.Mat2BufImg(month,".jpg");
        String monthStr= OCRUtil.getImageMessage(monthBuffer,"eng");

        //日期
        Point dayPoint1=new Point(birthday.cols()*0.69,0);
        Point dayPoint2=new Point(birthday.cols()*0.69,birthday.rows());
        Point dayPoint3=new Point(birthday.cols()*0.80,0);
        Point dayPoint4=new Point(birthday.cols()*0.80,birthday.rows());
        List<Point> dayList=new ArrayList<>();
        dayList.add(dayPoint1);
        dayList.add(dayPoint2);
        dayList.add(dayPoint3);
        dayList.add(dayPoint4);
        Mat day= shear(birthday,dayList);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/day.jpg", day);
        BufferedImage dayBuffer= OpencvUtil.Mat2BufImg(day,".jpg");
        String dayStr= OCRUtil.getImageMessage(dayBuffer,"eng");

        String birthdayStr=yearStr+"年"+monthStr+"月"+dayStr+"日";
        birthdayStr=birthdayStr.replace("\n","");
        return birthdayStr+"\n";
    }

    public static String address(Mat mat){
        Point point1=new Point(mat.cols()*0.17,mat.rows()*0.47);
        Point point2=new Point(mat.cols()*0.17,mat.rows()*0.47);
        Point point3=new Point(mat.cols()*0.61,mat.rows()*0.76);
        Point point4=new Point(mat.cols()*0.61,mat.rows()*0.76);
        List<Point> list=new ArrayList<>();
        list.add(point1);
        list.add(point2);
        list.add(point3);
        list.add(point4);
        Mat address= shear(mat,list);
        address= OpencvUtil.drawContours(address,50);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/address.jpg", address);
        BufferedImage addressBuffer= OpencvUtil.Mat2BufImg(address,".jpg");
        return OCRUtil.getImageMessage(addressBuffer,"chi_sim")+"\n";
    }

    public static String card(Mat mat){
        Point point1=new Point(mat.cols()*0.34,mat.rows()*0.75);
        Point point2=new Point(mat.cols()*0.34,mat.rows()*0.75);
        Point point3=new Point(mat.cols()*0.89,mat.rows()*0.91);
        Point point4=new Point(mat.cols()*0.89,mat.rows()*0.91);
        List<Point> list=new ArrayList<>();
        list.add(point1);
        list.add(point2);
        list.add(point3);
        list.add(point4);
        Mat card= shear(mat,list);
        card= OpencvUtil.drawContours(card,50);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/card.jpg", card);
        BufferedImage cardBuffer= OpencvUtil.Mat2BufImg(card,".jpg");
        return OCRUtil.getImageMessage(cardBuffer,"eng")+"\n";
    }



}
